package com.xiaohao.communication.EScontroller;

import com.xiaohao.communication.ESdao.ModernLiteratureSearch;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;


@RestController
public class ScrollController {
    private final ModernLiteratureSearch modernLiteratureSearch;

    @Autowired
    public ScrollController(ModernLiteratureSearch modernLiteratureSearch) {
        this.modernLiteratureSearch = modernLiteratureSearch;
    }

    /*
     * 第一次点击搜索时发送的请求
     * @param keyword
     * */
    @GetMapping("/scrollSearchFirstly")
    public SearchResponse scrollSearchFirstly(String keyword) {
        SearchResponse searchResponse = null;
        try {
            searchResponse = modernLiteratureSearch.scrollSearchFirstly(keyword);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return searchResponse;
    }

    /*
     * 点击下一页搜索时发送的请求
     * */
    @GetMapping("/scrollSearchSecondly")
    public SearchResponse scrollSearchSecondly(String scrollId) {
        SearchResponse searchResponse = null;
        try {
            searchResponse = modernLiteratureSearch.scrollSearchSecondly(scrollId);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return searchResponse;
    }

    /*
     *  看到查询出来的信息后，点击查看详细的信息。
     * */
    @GetMapping("/findByDocIndexTypeId")
    public GetResponse findByDocIndexTypeId(String index, String type, String id) {
        GetResponse getResponse = null;

        try {
            getResponse = modernLiteratureSearch.findById(index, type, id);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return getResponse;
    }


}
